var currentPage = 1;//当前页码
var pageSize = 5;//每页显示的数据量
var totalPage = 1;//记录总页码
var totalCount = 0;


function selLevel1() {
    let queryCategoryLevel1 = $("#queryCategoryLevel1").val();
    $.ajaxSettings.async = false;
    if (queryCategoryLevel1 != '' && queryCategoryLevel1 != null) {
        let json = {
            pid: queryCategoryLevel1
        }
        $.getJSON("/backend/level", json, function (data) {
            $("#queryCategoryLevel2").html("");
            $("#queryCategoryLevel2").append("<option value=\"\">--请选择--</option>");
            $(data).each(function (i, j) {
                $("#queryCategoryLevel2").append("<option value=\"" + j.id + "\">" + j.categoryName + "</option>");
            });
        })

    } else {
        $("#queryCategoryLevel2").html("");
        $("#queryCategoryLevel2").append("<option value=\"\">--请选择--</option>");
    }
    $("#queryCategoryLevel3").html("");
    $("#queryCategoryLevel3").append("<option value=\"\">--请选择--</option>");
    $.ajaxSettings.async = true;
}


function selLevel2() {
    let queryCategoryLevel2 = $("#queryCategoryLevel2").val();
    $.ajaxSettings.async = false;

    if (queryCategoryLevel2 != '' && queryCategoryLevel2 != null) {
        let json = {
            pid: queryCategoryLevel2
        }
        $.getJSON("/backend/level", json, function (data) {
            console.log(data)
            $("#queryCategoryLevel3").html("");
            $("#queryCategoryLevel3").append("<option value=\"\">--请选择--</option>");
            $(data).each(function (i, j) {
                $("#queryCategoryLevel3").append("<option value=\"" + j.id + "\">" + j.categoryName + "</option>");
            });
        })
    } else {
        $("#queryCategoryLevel3").html("");
        $("#queryCategoryLevel3").append("<option value=\"\">--请选择--</option>");
    }
    $.ajaxSettings.async = true;
}


function selAppList() {
    let softwareName = $("#softwareName").val();
    let flatformId = $("#flatformId").val();
    let queryCategoryLevel1 = $("#queryCategoryLevel1").val();
    let queryCategoryLevel2 = $("#queryCategoryLevel2").val();
    let queryCategoryLevel3 = $("#queryCategoryLevel3").val();

    $("#selAppsInfoList").find("tr").find("td").html("&nbsp;");

    $.ajaxSettings.async = false;
    let json = {
        softwareName: softwareName,
        flatformId: flatformId,
        level1: queryCategoryLevel1,
        level2: queryCategoryLevel2,
        level3: queryCategoryLevel3,
        currentPage: currentPage,
        pageSize: pageSize
    }

    $.getJSON("/backend/backendappinfolist", json, function (data) {
        totalPage = data.pages;
        totalCount = data.total;
        $(data.list).each(function (i, j) {
            $("#selAppsInfoList").find("tr").eq(i).find("td").eq(0).text(j.softwareName)
            $("#selAppsInfoList").find("tr").eq(i).find("td").eq(1).text(j.apkname)
            $("#selAppsInfoList").find("tr").eq(i).find("td").eq(2).text(j.softwareSize)
            $("#selAppsInfoList").find("tr").eq(i).find("td").eq(3).text(j.valueNameName)
            $("#selAppsInfoList").find("tr").eq(i).find("td").eq(4).text(j.categoryLevelName1 + "->" + j.categoryLevelName2 + "->" + j.categoryLevelName3)
            $("#selAppsInfoList").find("tr").eq(i).find("td").eq(5).text(j.statusName)
            $("#selAppsInfoList").find("tr").eq(i).find("td").eq(6).text(j.downloads)
            $("#selAppsInfoList").find("tr").eq(i).find("td").eq(7).text(j.versionNo)
            $("#selAppsInfoList").find("tr").eq(i).find("td").eq(8).html("<button type=\"button\" class=\"btn btn-default checkApp\" data-toggle=\"tooltip\" onclick=\"checkApp("+j.id+","+j.versionId+")\" data-placement=\"top\" title=\"\" data-original-title=\"查看并审核APP\">审核</button>")
        });
    })
    $.ajaxSettings.async = true;
    showPage();

}


function showPage() {
    $(".dataTables_info").text("共" + totalCount + "条记录" + currentPage + "/" + totalPage + "页");
    if (currentPage > 1) {
        $(".pagination").html("<li class=\"paginate_button previous\"><a href=\"javascript:;\" onclick=\"first()\" aria-controls=\"datatable-responsive\" data-dt-idx=\"0\" tabindex=\"0\">首页</a></li>\n" +
            "\t\t\t\t\t\t\t\t\t<li class=\"paginate_button \"><a href=\"javascript:;\" onclick=\"prev()\" aria-controls=\"datatable-responsive\" data-dt-idx=\"1\" tabindex=\"0\">上一页</a></li>");
    }
    if (currentPage < totalPage) {
        $(".pagination").html("<li class=\"paginate_button \"><a href=\"javascript:;\" aria-controls=\"datatable-responsive\" onclick=\"next()\" data-dt-idx=\"1\" tabindex=\"0\">下一页</a></li>\n" +
            "\t\t\t\t\t\t\t\t\t<li class=\"paginate_button next\"><a href=\"javascript:;\" aria-controls=\"datatable-responsive\" onclick=\"end()\" data-dt-idx=\"7\" tabindex=\"0\">最后一页</a></li>");
    }
}


function first() {
    currentPage = 1;
    selAppList();
}

function next() {
    currentPage++;
    if (currentPage > totalPage) {
        return;
    }
    selAppList();
}

function prev() {
    currentPage--;
    if (currentPage < 1) {
        return;
    }
    selAppList();
}

function end() {
    currentPage = totalPage;
    selAppList();
}

function checkApp(aid,versionId) {

    if(typeof versionId=="undefined"){
        versionId = 0;
    }
    window.location.href = "/backend/appcheck/" + aid +"/" + versionId;
}

